import wx
import math
import datetime

class ClockWindow(wx.Window):
    def __init__(self, *args, **kw):
        super(ClockWindow, self).__init__(*args, **kw)
        self.timer = wx.Timer(self) # 创建一个定时器
        self.Bind(wx.EVT_PAINT, self.on_paint) # 绑定OnPaint事件
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer) # 绑定OnTimer事件
        self.timer.Start(1000)  # 每秒刷新一次

    # 重写OnPaint事件
    def on_paint(self, event):
        dc = wx.PaintDC(self)  # 创建绘图上下文
        size = self.GetClientSize()  # 获取窗口大小
        radius = min(size.width, size.height) // 2 - 10  # 计算表盘半径
        center = wx.Point(size.width // 2, size.height // 2)  # 计算表盘中心点

        # 绘制表盘
        dc.SetPen(wx.Pen(wx.BLACK, 2))  # 设置画笔和画刷
        dc.SetBrush(wx.Brush(wx.WHITE))  # 设置画刷的颜色
        dc.DrawCircle(center, radius)  # 画一个圆

        # 绘制刻度
        for i in range(12):
            angle = math.radians(30 * i - 90)  # 计算角度
            start = wx.Point(  # 计算刻度起点
                int(center.x + (radius - 10) * math.cos(angle)),
                int(center.y + (radius - 10) * math.sin(angle))
            )
            end = wx.Point( # 计算刻度终点
                int(center.x + radius * math.cos(angle)),
                int(center.y + radius * math.sin(angle))
            )
            dc.DrawLine(start, end)

        # 获取当前时间
        now = datetime.datetime.now()
        hour = now.hour % 12
        minute = now.minute
        second = now.second

        # 绘制时针
        hour_angle = math.radians(30 * hour + 0.5 * minute - 90)  # 计算时针角度
        hour_length = radius * 0.5
        hour_end = wx.Point(
            int(center.x + hour_length * math.cos(hour_angle)),
            int(center.y + hour_length * math.sin(hour_angle))
        )
        dc.SetPen(wx.Pen(wx.BLACK, 4))
        dc.DrawLine(center, hour_end)

        # 绘制分针
        minute_angle = math.radians(6 * minute + 0.1 * second - 90)
        minute_length = radius * 0.7
        minute_end = wx.Point(
            int(center.x + minute_length * math.cos(minute_angle)),
            int(center.y + minute_length * math.sin(minute_angle))
        )
        dc.SetPen(wx.Pen(wx.BLACK, 3))
        dc.DrawLine(center, minute_end)

        # 绘制秒针
        second_angle = math.radians(6 * second - 90)
        second_length = radius * 0.8
        second_end = wx.Point(
            int(center.x + second_length * math.cos(second_angle)),
            int(center.y + second_length * math.sin(second_angle))
        )
        dc.SetPen(wx.Pen(wx.RED, 2))
        dc.DrawLine(center, second_end)

    # 重写OnTimer事件
    # 每秒刷新一次
    def on_timer(self, event):
        self.Refresh()  # 刷新窗口，触发重绘

class ClockFrame(wx.Frame):
    def __init__(self, *args, **kw):
        super(ClockFrame, self).__init__(*args, **kw)
        self.clock = ClockWindow(self)
        self.SetSize((400, 400))
        self.SetTitle("wxPython 时钟")
        self.Centre()

if __name__ == "__main__":
    app = wx.App(False)
    frame = ClockFrame(None)
    frame.Show()
    app.MainLoop()
